Associated table query function implemented by Mongodb [population method]

  • 2020-06-23 02:11:16
  • OfStack

This article illustrates the associated table query function implemented by Mongodb. To share for your reference, specific as follows:

Population

MongoDB is a non-associated database. But sometimes we want to refer to other documents. This is where the population comes in.

Population replaces a specific path in a document from another document. We can migrate a single 1 file, multiple files, normal objects, multiple normal objects, or all objects returned from a query

populate method

The populate method can be used on document, model, or query objects, which means you can call it almost anywhere to populate your reference fields.

Of course, the populate method does not have a lot of arguments on different objects, but each takes one option argument, which you can specify:

Grammar: Query.populate(path, [select], [model], [match], [options])

path: The name of the reference field String or Object, separated by Spaces,
When of type String, specify the associated fields to be populated, and multiple associated fields to be populated can be separated by Spaces.
Object encapsulates the parameters of populate into an object. It could be an array

select: Object or String, optionally specifying which fields to populate in document.
Type Object, such as: {name: 1, _id: 0}, 0 means no fill, 1 means fill.
Type String, such as "name -_id", with Spaces separating the fields and - before the field name to indicate no padding. Detailed syntax introduction to query-ES53en

match: Optionally, specify additional query criteria

model: Optionally, specify model for the associated field, or ref for Schema if not specified

options: Object is optional, specifying additional query options such as sorting and number limits, and so on

The type of the reference field

Currently, Mongoose supports only the following types of reference fields:

ObjectId
Number
String
Buffer

And, naturally, the primary key type that references document must correspond to the reference field type. In a production environment, ObjectId is recommended for both primary key types and reference types, 1 because ObjectId does not contain a business meaning, 2 because ObjectId is unlikely to be repeated, and 3 because Mongoose generates the primary key type by default as ObjectId, which reduces a lot of configuration operations.

example

Scheme defines the associated attributes first

AScheme:


var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var AScheme= new Schema({
  name: {
    type: String,
    default: ''
  }
},{collection: 'a'});
module.exports = mongoose.model('A', CountSchema);

BScheme:


var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var BScheme= new Schema({
  name: {
    type: String,
    default: ''
  },
  age:{
    type:Number
  }
  aid: {
     type: mongoose.Schema.ObjectId,// if AScheme the _id type for String And I'm going to write it here String
     ref:'A'
  }
},{collection: 'b'});
module.exports = mongoose.model('B', CountSchema);

Associated query


AModel.find().populate({path: 'aid', select: {name: 1,_id:0}}).exec(function(err, a) {
  if (err) res.send(err);
  console.log(a);
});
//
AModel.find(...).populate({ path: 'aid', match: { age: { $gte: 21 }}, select: 'name -_id', options: { limit: 5 } }) .exec()

More related content can be reference: http: / / mongoosejs com/docs/api html # model_Model. populate

I hope this article is helpful to the MongoDB database programming.


Related articles: